import pygame
from ui.button import Button

class BuildingList:
    def __init__(self, x, y, width, height):
        self.rect = pygame.Rect(x, y, width, height)
        self.buildings = []
        
        # 使用系统字体
        try:
            # Windows系统
            self.title_font = pygame.font.SysFont('Microsoft YaHei', 32)
            self.info_font = pygame.font.SysFont('Microsoft YaHei', 24)
        except:
            try:
                # Linux系统
                self.title_font = pygame.font.SysFont('WenQuanYi Micro Hei', 32)
                self.info_font = pygame.font.SysFont('WenQuanYi Micro Hei', 24)
            except:
                # 如果都失败，使用默认字体
                self.title_font = pygame.font.Font(None, 32)
                self.info_font = pygame.font.Font(None, 24)
    
    def update_buildings(self, buildings):
        self.buildings = buildings
    
    def draw(self, screen):
        # 绘制背景
        pygame.draw.rect(screen, (50, 50, 50), self.rect)
        
        # 绘制标题
        title = self.title_font.render("建筑列表", True, (255, 255, 255))
        screen.blit(title, (self.rect.x + 10, 10))
        
        # 绘制建筑列表
        y = 50
        for i, building in enumerate(self.buildings):
            # 绘制建筑序号
            index_text = self.info_font.render(f"{i+1}.", True, (255, 255, 255))
            screen.blit(index_text, (self.rect.x + 10, y))
            
            # 绘制建筑类型（根据颜色判断）
            building_type = "未知"
            if building.color == (200, 200, 200):
                building_type = "指挥中心"
            elif building.color == (150, 150, 150):
                building_type = "兵营"
            elif building.color == (100, 100, 100):
                building_type = "补给站"
            elif building.color == (200, 150, 100):
                building_type = "工程站"
            elif building.color == (100, 200, 100):
                building_type = "精炼厂"
                
            type_text = self.info_font.render(building_type, True, (200, 200, 200))
            screen.blit(type_text, (self.rect.x + 40, y))
            
            # 绘制位置和大小信息
            info_text = self.info_font.render(
                f"位置: ({building.grid_x}, {building.grid_y})",
                True, (150, 150, 150)
            )
            screen.blit(info_text, (self.rect.x + 10, y + 25))
            
            size_text = self.info_font.render(
                f"大小: {building.width}x{building.height}",
                True, (150, 150, 150)
            )
            screen.blit(size_text, (self.rect.x + 10, y + 50))
            
            # 绘制分隔线
            pygame.draw.line(
                screen, (100, 100, 100),
                (self.rect.x + 10, y + 75),
                (self.rect.x + self.rect.width - 10, y + 75)
            )
            y += 90  # 增加间距